/**
 * 礼物类 by chx
 */
import { createDom } from '@/utils'
import EventEmitter from './EventEmitter'
export default class Gift{
	constructor(giftInfo,container) {
		this.giftInfo = giftInfo
		this.emitter = new EventEmitter()
		this.giftHideTime = 2500
		this.container = container
	}
	createGift(){
		this.giftDom = createDom('div',`<span class="from_nick_name ell" style="margin-right: 5px;"></span><span>给<span class="teacher_name"></span><span class="to_nick_name ell"></span>送了<span class="gift_name"></span></span><i class="icon-gift"></i><div class="giftNum">x0</div>`,{},'hjPopGift hjPopGift_small first')
		this.giftDom.querySelector('.hjPopGift_small.first .icon-gift').style.backgroundImage = `url(${this.giftInfo.gift_thumb_img_v3})`
		this.giftDom.querySelector('.hjPopGift_small.first .gift_name').innerText = this.giftName
		this.giftDom.querySelector('.hjPopGift_small.first .from_nick_name').innerText = this.gitFromNickName
		this.giftDom.querySelector('.hjPopGift_small.first .to_nick_name').innerText = this.giftToNickName
		this.giftDom.querySelector('.giftNum').innerText = 'x' + this.sum
		this.container.appendChild(this.giftDom)
		setTimeout(()=>{
			this.giftDom.classList.add('active')
		},0)
	}
	highlightGift(){
		this.giftDom.querySelector('.giftNum').innerText = 'x' + this.sum
		this.giftDom.querySelector('.giftNum').classList.add('active')
		setTimeout(()=>{
			this.giftDom.querySelector('.giftNum').classList.remove('active')
		},50)
	}
	updateGiftTop(top){
		if(this.giftDom){
			this.giftDom.style.top = top
		}
	}
	detroyGift(){
		this.giftDom.classList.remove('active')
		let transitionFlag = true
		this.giftDom.addEventListener('transitionend', (e) => {
			if(transitionFlag && e.target === this.giftDom && this.giftDom.parentNode){
				this.giftDom.parentNode.removeChild(this.giftDom)
				transitionFlag = false
				this.destroyed = true
				this.emitter.trigger('destroyed')
			}
		});
	}
	get sum(){
		if(this.giftInfo){
			return this.giftInfo.sum
		}
	}
	set sum(n){
		if(this.giftInfo){
			this.giftInfo.sum = n
		}
	}
	get giftId(){
		if(this.giftInfo){
			return this.giftInfo.id
		}
	}
	get giftFromUid(){
		if(this.giftInfo){
			return this.giftInfo.from_uid
		}
	}
	get giftToUid(){
		if(this.giftInfo){
			return this.giftInfo.to_uid
		}
	}
	get giftName(){
		if(this.giftInfo){
			return this.giftInfo.gift_name
		}
	}
	get gitFromNickName(){
		if(this.giftInfo){
			return this.giftInfo.from_nickname
		}
	}
	get giftToNickName(){
		if(this.giftInfo){
			return this.giftInfo.to_nickname
		}
	}
	on(event, listener) {
		this.emitter.on(event, listener)
	}
	off(event, listener) {
		this.emitter.off(event, listener)
	}
	once(event, listener){
		this.emitter.once(event, listener)
	}
}